-
Notifications
You must be signed in to change notification settings - Fork 252
[ONNX] Add eliminate_nop_cast pass #3376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
[ONNX] Add eliminate_nop_cast pass #3376
Conversation
@@ -348,6 +349,7 @@ def create_nncf_graph(onnx_model: onnx.ModelProto) -> NNCFGraph: | |||
""" | |||
onnx_model = GraphConverter._replace_empty_node_name(onnx_model) | |||
onnx_model = onnx.shape_inference.infer_shapes(onnx_model) | |||
onnx_model = onnxoptimizer.optimize(onnx_model, ["eliminate_nop_cast"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other passes that can be useful as well.
"eliminate_nop_dropout",
"eliminate_nop_flatten",
"eliminate_if_with_const_cond",
"eliminate_nop_monotone_argmax",
"eliminate_nop_pad",
"eliminate_nop_concat",
"eliminate_nop_split",
"eliminate_nop_expand",
"eliminate_shape_gather",
"eliminate_slice_after_shape",
"eliminate_nop_transpose",
"eliminate_nop_reshape",
"eliminate_nop_with_unit",
"eliminate_common_subexpression",
"eliminate_deadend",
"eliminate_identity",
"eliminate_shape_op",
"eliminate_unused_initializer",
"eliminate_duplicate_initializer",
onnx_model = onnx.shape_inference.infer_shapes(onnx_model) | ||
onnx_model = onnxoptimizer.optimize(onnx_model, ["eliminate_nop_cast"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pass should be applied after onnx.shape_inference.infer_shapes()
call. Otherwise, not all no-op Cast
nodes will be found.
@@ -348,6 +349,7 @@ def create_nncf_graph(onnx_model: onnx.ModelProto) -> NNCFGraph: | |||
""" | |||
onnx_model = GraphConverter._replace_empty_node_name(onnx_model) | |||
onnx_model = onnx.shape_inference.infer_shapes(onnx_model) | |||
onnx_model = onnxoptimizer.optimize(onnx_model, ["eliminate_nop_cast"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following code snippet can be used to apply that pass to the initial ONNX model.
import onnx
import onnxoptimizer
model = onnx.load(MODEL_PATH)
model = onnx.shape_inference.infer_shapes(model)
model = onnxoptimizer.optimize(model, ["eliminate_nop_cast"])
@kshpv Please review |
4650dc4
to
fa04951
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this PR in draft or can it be reviewed?
Changes
Apply the
eliminate_nop_cast
optimization pass fromonnxoptimizer
before building the NNCF graph. This pass aims to eliminate no-op cast nodes in the graph. A no-op cast is a cast that doesn't change the value, such as casting a tensor to its own type.Reason for changes
Statistics cannot be collected after the no-op Cast nodes because such nodes are removed from the ONNX inference graph during the session.
Related tickets
Ref: 164211